﻿Imports DTO
Imports System.Data.SqlClient


Public Class CTinhTrangSachDAO
    Public Function LayDanhSachTinhTrangSach() As List(Of CTinhTrangSachDTO)
        Using cn As SqlConnection = CDataProvider.Connect()
            Dim cm As New SqlCommand("uspLoadTinhTrangSach", cn)
            cm.CommandType = CommandType.StoredProcedure
            Dim listtts As New List(Of CTinhTrangSachDTO)()
            Using reader As IDataReader = cm.ExecuteReader(CommandBehavior.CloseConnection)
                While reader.Read()
                    listtts.Add(New CTinhTrangSachDTO(reader))
                End While
            End Using
            Return listtts
        End Using
    End Function

    Public Function ThemTinhTrangSach(ByVal tentinhtrangsach As String) As Integer
        Using cn As SqlConnection = CDataProvider.Connect()
            Dim cm As New SqlCommand("usp_ThemTinhTrangSach", cn)
            cm.CommandType = CommandType.StoredProcedure
            Dim para As New SqlParameter("@tentinhtrangsach", tentinhtrangsach)
            cm.Parameters.Add(para)
            Dim parakq As New SqlParameter("@kt", 0)
            parakq.Direction = ParameterDirection.Output
            cm.Parameters.Add(parakq)
            cm.ExecuteNonQuery()
            Dim kq As Integer = Integer.Parse(cm.Parameters("@kt").Value.ToString())
            cn.Close()
            Return kq
        End Using
    End Function


    Public Function CapNhatTinhTrangSach(ByVal tinhtrangsach As CTinhTrangSachDTO) As Integer
        Using cn As SqlConnection = CDataProvider.Connect()
            Dim cm As New SqlCommand("usp_CapNhatTinhTrangSach", cn)
            cm.CommandType = CommandType.StoredProcedure
            Dim para As New SqlParameter("@matinhtrangsach", tinhtrangsach.Ma)
            cm.Parameters.Add(para)
            para = New SqlParameter("@tentinhtrangsach", tinhtrangsach.Ten)
            cm.Parameters.Add(para)
            Dim parakt As New SqlParameter("@kt", 0)
            parakt.Direction = ParameterDirection.Output
            cm.Parameters.Add(parakt)

            cm.ExecuteNonQuery()
            Dim kq As Integer = Integer.Parse(cm.Parameters("@kt").Value.ToString())
            cn.Close()
            Return kq
        End Using
    End Function


End Class
